package jp.sourceforge.akjrcp.image.trimming;



import org.eclipse.draw2d.Button;
import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.FocusBorder;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.LineBorder;
import org.eclipse.draw2d.MouseEvent;
import org.eclipse.draw2d.MouseMotionListener;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.jface.resource.ColorRegistry;
import org.eclipse.swt.graphics.Image;

public class DrawImageFigure extends Figure {
private Image image;
private double zoom=1;
private int minWidth=100;
private int minHeight=100;
public DrawImageFigure(){
	super();
	setOpaque(false);//transparent
	//this.setEnabled(false);
	//this.setRequestFocusEnabled(false);
	//setBorder(new LineBorder()); //no for save
	setSize(1,1);
}



public double getZoom() {
	return zoom;
}

public void setZoom(double zoom) {
	this.zoom = zoom;
}

public void paintFigure(Graphics graphics) {
	if(image!=null){
	//TODO zoom
		
		if(zoom!=1){
		int w=(int) (image.getBounds().width*zoom);
		int h=(int) (image.getBounds().height*zoom);
		//System.out.println(w+","+h);
		graphics.drawImage(image,0,0,image.getBounds().width,image.getBounds().height,0,0,w,h);
		}else{
			graphics.drawImage(image,0,0);
		}
		
	}
	
}

public Dimension getPreferredSize(int wHint, int hHint) {
	if(image==null){
	return new Dimension(minWidth,minHeight);
	}else{
	return new Dimension(Math.max(minWidth, image.getBounds().width),Math.max(minHeight, image.getBounds().height));
	}
}

public Image getImage() {
	return image;
}



public void setImage(Image image) {
	this.image = image;
	if(image!=null){
	this.setSize(image.getBounds().width,image.getBounds().height);
	}else{
	this.setSize(minWidth,minHeight);
	}
	//this.validate();
	//this.layout();
	//this.repaint(getBounds());
	revalidate();
}


}
